﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class InstrumentComponentAdd : BasePage
    {
        private SystemBll sysBll = new SystemBll();

        public Guid InstrumentID
        {
            get
            {
                if (hiddenInstrumentID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenInstrumentID.Value);
            }
            set
            {
                hiddenInstrumentID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initForm();
            }
        }

        private void initForm()
        {
            if (Request.QueryString["InsturmentID"] != null && Request.QueryString["InsturmentID"] != "")
            {
                InstrumentID = new Guid(Request.QueryString["InsturmentID"].ToString());
            }
        }

        private void save()
        {
            WsmComponentInstrumentEntity entity = new WsmComponentInstrumentEntity();
            if (InstrumentID != Guid.Empty)
            {
                List<WsmComponentInstrumentEntity> entitys = sysBll.GetAll<WsmComponentInstrumentEntity>(" and ComponentID = '" + hiddenComponentID.Value + "' and InstrumentID='" + InstrumentID.ToString() + "'");
                if (entitys != null && entitys.Count > 0)
                {
                    return;
                }
                if (hiddenComponentID.Value.Trim() != "")
                {
                    entity.ID = Guid.NewGuid();
                    entity.ComponentID = new Guid(hiddenComponentID.Value);
                    entity.InstrumentID = InstrumentID;
                    sysBll.Insert(entity);
                }
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            save();
            hiddenComponentID.Value = "";
            hiddenComponentName.InnerHtml = "";
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');", true);
        }
    }
}
